package com.codename1.samples;


import com.codename1.components.MultiButton;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.ui.Button;
import com.codename1.ui.Container;
import com.codename1.ui.FontImage;
import com.codename1.ui.TextArea;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.layouts.BorderLayout;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class JavascriptScrollingTest {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        new FrmSchedule().show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }
    
    
    public class FrmSchedule extends com.codename1.ui.Form {

    	// UI elements
    	Container cntList = new Container(new BoxLayout(BoxLayout.Y_AXIS));

    	/* Constructor
    	 * 
    	 */
    	public FrmSchedule() {
    		initUI();
    	}
    	
    	
    	/* initManualComponents()
    	 * inits the ui
    	 */
    	private void initUI() {
    		setLayout(new BorderLayout());
    		setTitle("Speaches");
    		setName("FrmPTSchedule");
    		
    		// changes to UI elements
    		cntList.setScrollableY(true);

    		// build the form
    		buildSideMenu();
    		buildTitle();
    		
    		// add it all to the form
    		this.add(BorderLayout.CENTER, cntList);
    		
    		// retrieve and build the list of the schedule
    		setValues();
    	}
    	
    	
    	// retrieve the schedule from the server and fill the MBs with the data
    	protected void setValues() {
    		cntList.removeAll();
    		for (int x=0; x<15; x++) {
    			MultiButton mb = new MultiButton("Line " + x);
    	        cntList.add(mb);
    		}

    		revalidate();
    	}
    	
    	protected void onTest() {
    		Dialog dlg = new Dialog("Test");
    		dlg.setLayout(new BorderLayout());
    		TextArea taMsg = new TextArea();
    		dlg.add(BorderLayout.CENTER, taMsg);
    		Button btnOk = new Button("Ok");
    		btnOk.addActionListener(e -> dlg.dispose());
    		dlg.add(BorderLayout.SOUTH, btnOk);
    		taMsg.setText("This is a test Message");
    		taMsg.setGrowLimit(-1);
    		taMsg.getAllStyles().setAlignment(TextArea.LEFT);
    		dlg.show();

    	}
    	
    	protected void buildTitle() {
    		// fix the title so we can add the date chooser to it
    		Button btnTest = new Button("Click Me");
    		btnTest.addActionListener(l->onTest());
    		Label lblTitle = new Label("Speach");
    		lblTitle.getAllStyles().setAlignment(CENTER);

    		Container cntTitle = new Container(new BorderLayout());
    		cntTitle.add(BorderLayout.WEST, btnTest);
    		cntTitle.add(BorderLayout.CENTER, lblTitle);

    		getToolbar().setTitleCentered(false);
    		getToolbar().setTitleComponent(cntTitle);
    	}
    	
    	// builds the side menu
    	protected void buildSideMenu() {
    		// sidebar
    		if (getToolbar() == null) {
    			setToolbar(new Toolbar());
    		}
    		Toolbar tb = getToolbar();
    		Container topBar = new Container (new BorderLayout());
    		topBar.add(BorderLayout.SOUTH, new Label("Title", "SidemenuTagline")); 
    		topBar.setUIID("SideCommand");
    		tb.addComponentToSideMenu(topBar);

    		tb.addMaterialCommandToLeftSideMenu("Place", FontImage.MATERIAL_HOME, e->onMenu(e, "Place"));
    		tb.addMaterialCommandToLeftSideMenu("People", FontImage.MATERIAL_PERSON, e->onMenu(e, "People"));
    		tb.addMaterialCommandToLeftSideMenu("Schedule", FontImage.MATERIAL_SCHOOL, e->onMenu(e, "Schedule"));
    	}
    	protected void onMenu(ActionEvent ev, String cmd) {
    	}

    }

}
